package cn.calm.xhtml.impl.html;

import java.util.Map;
import java.util.Map.Entry;

import cn.calm.xhtml.api.NodeProcess;
import cn.calm.xhtml.api.exception.XhtmlException;
import cn.calm.xhtml.api.service.AbstraceNodeProcess;
import cn.calm.xhtml.api.service.ResponseWriter;
import cn.calm.xhtml.api.service.XhtmlContext;
import cn.calm.xhtml.util.ValueUtil;

public class Default extends AbstraceNodeProcess {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void processNode(XhtmlContext context) throws XhtmlException {
		ResponseWriter writer2 = context.getResponseWriter();
		writer2.startTag(getName());
		processAttr(context);
		writer2.write(">");
		NodeProcess[] children = getChildren();
		if (children == null || children.length == 0) {
			writer2.write(ValueUtil.getValue(getContent(), context));
		} else {
			processChildren(context);
		}
		writer2.endTag(getName());
	}

	private void processAttr(XhtmlContext context) throws XhtmlException {
		Map<String, String> attr = getAttr();
		ResponseWriter writer2 = context.getResponseWriter();
		for (Entry<String, String> a : attr.entrySet()) {
			writer2.writeAttr(a.getKey(),
					ValueUtil.getValue(a.getValue(), context));
		}
	}

}
